uint8_t I2C_Write_x(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint8_t add,uint8_t *pData,uint8_t x,bool prologue_flag)
{
	DevAddress=(DevAddress<<1)&0xFF;
	if(prologue_flag)
	{
		HAL_I2C_Mem_Write(hi2c,DevAddress,add,I2C_MEMADD_SIZE_8BIT,pData,x,0xFFFF);
	}	
	else
	{
		HAL_I2C_Master_Transmit(hi2c,DevAddress,pData,x,0xFFFF);
	}
	
	return 0;
}

uint32_t I2C_Read_y(I2C_HandleTypeDef *hi2c,uint16_t DevAddress,uint8_t add,uint8_t y,bool prologue_flag)
{
	DevAddress=(DevAddress<<1)&0xFF;
	uint8_t pData[y];
	memset(pData,0,sizeof(pData));
	uint32_t dat=0;
	
	if(y>4)
	{
		return 0;
	}
	
	if(prologue_flag)
	{
		HAL_I2C_Mem_Read(hi2c,DevAddress,add,I2C_MEMADD_SIZE_8BIT,pData,y,0xFFFF);
	}	
	else
	{
		HAL_I2C_Master_Receive(hi2c,DevAddress,pData,y,0xFFFF);
	}
	
	for(uint8_t i=0;i<y;i++)
	{
		dat=(dat<<(8*i))|pData[i];
	}
	
	return dat;
}

uint32_t SPI_Send_x_Read_y(SPI_HandleTypeDef *hspi, uint8_t *pData, uint8_t x,uint8_t y,bool sync_flag)
{
	HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_SET);		
	
	uint8_t buf[x+y];
	memset(buf,0,sizeof(buf));
	uint32_t dat=0;
	
	if(y>4)
	{
		return 0;
	}
	
	if(sync_flag)
	{
		HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_RESET);
		HAL_SPI_TransmitReceive(hspi,pData,buf,x+y,0xFFFF);	
		HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_SET);		
	}
	else
	{
		HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_RESET);
		HAL_SPI_Transmit(hspi,pData,x,0xFFFF);
		HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_SET);		
		HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_RESET);
		HAL_SPI_Receive(hspi,buf,y,0xFFFF);
		HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_SET);
		x=0;
	}
	
	for(uint8_t i=0;i<y;i++)
	{
		dat=(dat<<(8*i))|buf[x+i];
	}
	
	HAL_GPIO_WritePin(SPI1_CS0_GPIO_Port, SPI1_CS0_Pin, GPIO_PIN_SET);
	
	return dat;
}

void init_ADXL363(void)
{
	uint8_t buf[3];
	buf[0]=WRITE_REGISTER;	
	buf[1]=FIFO_CONTROL;
	buf[2]=0x0E;
	SPI_Send_x_Read_y(&hspi1,buf,3,0,true);
	buf[1]=POWER_CTL;
	buf[2]=0x02;
	SPI_Send_x_Read_y(&hspi1,buf,3,0,true);
	buf[1]=FIFO_CONTROL;
	buf[2]=0x0E;
	SPI_Send_x_Read_y(&hspi1,buf,3,0,true);
	buf[1]=POWER_CTL;
	buf[2]=0x02;
	SPI_Send_x_Read_y(&hspi1,buf,3,0,true);
}

void count_ADXL363(void)
{
	uint8_t buf[2];
	
	uint8_t data_H = 0;
	uint8_t data_L = 0;
	uint16_t data_x = 0;
	uint16_t data_y = 0;
	uint16_t data_z = 0;
	uint16_t data_t = 0;

	float real_x = 0.0;
	float real_y = 0.0;
	float real_z = 0.0;
	float real_t = 0.0;
	
	buf[0]=READ_REGISTER;
	buf[1]=XDATA_H;
	data_H=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	buf[1]=XDATA_L;
	data_L=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	
	data_H = data_H & 0x0F;
	data_L = data_L & 0xFF;
	
	data_x = ((data_H<<8)+data_L)&0xFFF;
	
	if(((data_H>>3)&0xFF)>0)
	{
		data_x = 0x1000 - data_x;
		real_x = -(data_x/1000.0);
	}
	else
	{
		real_x = data_x/1000.0;
	}
	data_H = 0;
	data_L = 0;
	
	buf[1]=YDATA_H;
	data_H=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	buf[1]=YDATA_L;
	data_L=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	
	data_H = data_H & 0x0F;
	data_L = data_L & 0xFF;
	
	data_y = ((data_H<<8)+data_L)&0xFFF;
	
	if(((data_H>>3)&0xFF)>0)
	{
		data_y = 0x1000 - data_y;
		real_y = -(data_y/1000.0);
	}
	else
	{
		real_y = data_y/1000.0;
	}
	data_H = 0;
	data_L = 0;
	
	
	buf[1]=ZDATA_H;
	data_H=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	buf[1]=ZDATA_L;
	data_L=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	
	data_H = data_H & 0x0F;
	data_L = data_L & 0xFF;
	
	data_z = ((data_H<<8)+data_L)&0xFFF;
	
	if(((data_H>>3)&0xFF)>0)
	{
		real_z = -(data_z/1000.0);
	}
	else
	{
		real_z = data_z/1000.0;
	}
	data_H = 0;
	data_L = 0;
	
	
	buf[1]=TEMP_H;
	data_H=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	buf[1]=TEMP_L;
	data_L=SPI_Send_x_Read_y(&hspi1,buf,2,1,true);
	
	data_H = data_H & 0x0F;
	data_L = data_L & 0xFF;
	
	data_t = ((data_H<<8)+data_L)&0xFFF;
	
	if(((data_H>>3)&0xFF)>0)
	{
		data_t = 0x1000 - data_t;
		real_t = -(data_t*0.25);
	}
	else
	{
		data_t = data_t;
		real_t = data_t*0.25;
	}
	data_H = 0;
	data_L = 0;
	
	printf("[INFO] X:%0.4f,Y:%0.4f,Z:%0.4f,T:%0.2f\n",real_x,real_y,real_z,real_t);
}

uint16_t read_ADT7420(void)
{	
	uint16_t tmp=0;
	tmp=I2C_Read_y(&hi2c1,ADT7420_Slave_Add,0x00,2,true);
	tmp=(tmp>>3)&0xFFFF;
		
	return tmp;
}

float count_ADT7420(void)
{
	float real_tmp =0;
	uint16_t tmp = read_ADT7420();
	if (tmp<0x1000)
	{
		real_tmp = tmp*0.0625;
	}
	else if (tmp>=0x1000 && tmp <0x2000)
	{
		tmp = 0x2000 - tmp;
		real_tmp = -(tmp*0.0625);
	}
	else
	{
		real_tmp = -273.15;
	}
	
	printf("[INFO] ADT7420_Temp:%0.4f\n",real_tmp);
	return real_tmp;
}

